/*
 * @Author: heinan
 * @Date: 2023-08-01 18:32:40
 * @Last Modified by: heinan
 * @Last Modified time: 2023-08-01 21:38:20
 */
"use strict";
const Controller = require("egg").Controller;
/**
 *  @Controller 部门管理+员工管理
 */
class Staff extends Controller {
  /**
   * @Summary 获取部门列表
   * @Description 获取部门列表
   * @Router GET /staff/portList
   */
  async Portlist() {
    const { ctx } = this;
    const data = await this.app.mysql.query(`SELECT * FROM port`);
    ctx.body = {
      code: 200,
      data: data.length ? data : false,
    };
  }

  /**
   * @Summary 删除部门
   * @Description 根据部门id删除部门
   * @Router GET /staff/portDel
   * @Request query string id 部门ID
   */
  async delPort() {
    const { ctx } = this;
    const { id } = ctx.query;
    const result = await this.app.mysql.query(
      `delete from port where id = '${id}'`
    );
    if (result.affectedRows == 1) {
      ctx.body = {
        code: 200,
        message: "删除成功",
      };
    } else {
      ctx.body = {
        code: 401,
        message: "删除失败",
      };
    }
  }
  /**
   * @Summary 添加部门
   * @Description 添加部门
   * @Router POST /staff/portAdd
   * @Request body portCreateRequest *body
   */
  async PortAdd() {
    const { ctx } = this;
    const { name, num, charge, job, tel } = ctx.request.body;
    const result = await this.app.mysql.query(
      `insert into port (name,num,charge,job,tel) values ('${name}','${num}','${charge}','${job}',${tel})`
    );
    if (result.affectedRows == 1) {
      ctx.body = {
        code: 200,
        message: "添加成功",
      };
    } else {
      ctx.body = {
        code: 401,
        message: "添加失败",
      };
    }
  }
  /**
   * @Summary 编辑部门
   * @Description 编辑部门
   * @Router POST /staff/portEdit
   * @Request body portUpdateRequest *body
   */
  async PortEdit() {
    const { ctx } = this;
    const { id, name, num, charge, job, tel } = ctx.request.body;
    const result = await this.app.mysql.update("port", {
      id,
      name,
      num,
      charge,
      job,
      tel,
    });
    if (result.affectedRows == 1) {
      ctx.body = {
        code: 200,
        message: "编辑成功",
      };
    } else {
      ctx.body = {
        code: 401,
        message: "编辑失败",
      };
    }
  }
  /**
   * @Summary 查找部门
   * @Description 查找部门
   * @Router GET /staff/portSearch
   * @Request query string searchVal 搜索关键字
   */
  async portSearch() {
    const { ctx } = this;
    const { searchVal } = ctx.query;
    const result = await this.app.mysql.query(
      `select * from port where name = '${searchVal}'`
    );
    if (result.length >= 1) {
      ctx.body = {
        code: 200,
        data: result,
      };
    } else {
      ctx.body = {
        code: 401,
        message: "查找失败",
      };
    }
  }

  //员工管理
  /**
   * @Summary 获取员工管理列表
   * @Description 获取员工管理列表
   * @Router GET /staff/staffList
   */
  async Stafflist() {
    const { ctx } = this;
    const data = await this.app.mysql.query(`SELECT * FROM staff`);
    ctx.body = {
      code: 200,
      data: data.length ? data : false,
    };
  }
  /**
   * @Summary 删除员工
   * @Description 根据员工id删除员工
   * @Router GET /staff/staffDel
   * @Request query string id 员工ID
   */
  async delStaff() {
    const { ctx } = this;
    const { id } = ctx.query;
    const result = await this.app.mysql.query(
      `delete from staff where id = '${id}'`
    );
    if (result.affectedRows == 1) {
      ctx.body = {
        code: 200,
        message: "删除成功",
      };
    } else {
      ctx.body = {
        code: 401,
        message: "删除失败",
      };
    }
  }
  /**
   * @Summary 添加员工
   * @Description 添加员工
   * @Router POST /staff/staffAdd
   * @Request body staffCreateRequest *body
   */
  async StaffAdd() {
    const { ctx } = this;
    const password = Math.random().toString().substring(3, 9);
    const { name, jobNum, job, tel, port } = ctx.request.body;
    const result = await this.app.mysql.query(
      `insert into staff (name,jobNum,password,job,tel,port) values ('${name}','${jobNum}','${password}','${job}','${tel}','${port}')`
    );
    if (result.affectedRows == 1) {
      ctx.body = {
        code: 200,
        message: "添加成功",
      };
    } else {
      ctx.body = {
        code: 401,
        message: "添加失败",
      };
    }
  }
  /**
   * @Summary 编辑员工
   * @Description 编辑员工
   * @Router POST /staff/staffEdit
   * @Request body staffUpdateRequest *body
   */
  async StaffEdit() {
    const { ctx } = this;
    const { id, name, jobNum, password, job, tel, port } = ctx.request.body;
    const result = await this.app.mysql.update("staff", {
      id,
      name,
      jobNum,
      password,
      job,
      tel,
      port,
    });
    if (result.affectedRows == 1) {
      ctx.body = {
        code: 200,
        message: "编辑成功",
      };
    } else {
      ctx.body = {
        code: 401,
        message: "编辑失败",
      };
    }
  }
  /**
   * @Summary 查找员工
   * @Description 查找员工
   * @Router GET /staff/staffSearch
   * @Request query string searchVal 搜索关键字
   */
  async staffSearch() {
    const { ctx } = this;
    const { searchVal } = ctx.query;
    const result = await this.app.mysql.query(
      `select * from staff where name = '${searchVal}'`
    );
    if (result.length >= 1) {
      ctx.body = {
        code: 200,
        data: result,
      };
    } else {
      ctx.body = {
        code: 401,
        message: "查找失败",
      };
    }
  }
}
module.exports = Staff;

